package com.tiantian.novel.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 状态管理
 *
 * @author wangk21
 * @date 2023/11/12.
 */
@Slf4j
@RequiredArgsConstructor
public class StatusManager {
    /**
     * 进行中的任务
     */
    private static final Map<String, String> TASKS = new ConcurrentHashMap<>();

    /**
     * 给正在进行中的任务
     *
     * @param taskId taskId
     * @param value  url
     */
    public static boolean beginTask(String taskId, String value) {
        log.info("开始一项任务:{}-{}", taskId, value);
        if (TASKS.containsKey(taskId)) {
            return false;
        } else {
            TASKS.putIfAbsent(taskId, value);
            return true;
        }
    }

    /**
     * 完成一项任务
     *
     * @param taskId taskId
     */
    public static void finishTask(String taskId) {
        log.info("结束一项任务:{}", taskId);
        TASKS.remove(taskId);
    }
}
